<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>

</html>
<script>
    const data = [
        {
            "area_id": 5,
            "name": "广东省",
            "parent_id": 0,
        },
        {
            "area_id": 6,
            "name": "广州市",
            "parent_id": 5,
        },
        {
            "area_id": 7,
            "name": "深圳市",
            "parent_id": 5,
        },
        {
            "area_id": 4,
            "name": "北京市",
            "parent_id": 3,
        },
        {
            "area_id": 3,
            "name": "北京",
            "parent_id": 0,
        },
        {
            "area_id": 2,
            "name": "测试子地区",
            "parent_id": 1,
        },
        {
            "area_id": 1,
            "name": "测试地区",
            "parent_id": 0,
        }
    ]

    let toTree = (data) => {
        let treeArr = []
        function rescursion(data) {
            data.forEach(item => {
                if (item.parent_id) {
                    let parent = data.find(v => v.area_id === item.parent_id)
                    if (!parent.children) {
                        parent.children = []
                    }
                    parent.children.push(item)
                } else {
                    treeArr.push(item)
                }
            })
        }
        rescursion(data)
        return treeArr
    }

    console.log(toTree(data))
</script>